package com.study.core.security;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;

import java.util.*;

/**
 * Description
 *
 * @ClassName com.study.core.security.bean
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/7/12
 * @Modified by none
 * @Modified Date
 */

public class MyMetadataSourceService extends JdbcDaoImpl {
    //这边只查询了所有角色下的权限
    private String getAllResourceMapSql = "SELECT (SELECT url_name from tb_url where id = ur.url_id) as urlname,(SELECT role_name from tb_role where id = ur.role_id ) as rolename from tb_url_role as ur";

    //获取所有的url和权限的映射关系
    public Map getAllResourceMap(){
        Map<String,Collection<ConfigAttribute>>  resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
        List list = this.getJdbcTemplate().queryForList(getAllResourceMapSql);
        if(list.size()>0){
            GrantedAuthority grantedAuthority = null;
            Iterator it = list.iterator();
            while(it.hasNext()) {
                Map map = (Map) it.next();
                Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
                ConfigAttribute ca = new SecurityConfig((String)map.get("rolename"));
                atts.add(ca);
                resourceMap.put((String)map.get("urlname"), atts);
            }
        };
        return resourceMap;
    }
}
